﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Waker.Common;
using Waker.Entitys;
using System.Text.RegularExpressions;
using Waker.APIs.ZKT;
using Waker.Entitys;

namespace Waker.Cards
{
    public partial class FormFingerSetting : Base.FormBasePop
    {
        public FormFingerSetting()
        {
            InitializeComponent();
        }

        private void FormFingerSetting_Load(object sender, EventArgs e)
        {
            this.txtIP.Text = string.IsNullOrEmpty(Session.Finger_IP) ? "192.168.1.201" : Session.Finger_IP;
            spePort.EditValue = Session.Finger_Port < 1 ? 4370 : Session.Finger_Port;
        }
        protected override void FormSave()
        {
            if (SaveToINI())
            {
                UI.ShowInfo("保存成功");
                this.DialogResult = DialogResult.OK;
            }
            else
            {
                UI.ShowError("保存失败");
            }
        }

        private bool SaveToINI()
        {
            try
            {
                StartWaiting();
                sbtnReset.Enabled = sbtnSetTime.Enabled = false;
                if (!CheckInput())
                    return false;
                var res = TestConnection();
                if (!res.IsSuccess)
                {
                    lblStatus.Text = res.Message;
                    UI.ShowError("测试连接失败,请修改后再次保存");
                    return false;
                }
                lblStatus.Text = "已经连接";
                WriteToIni();
                sbtnReset.Enabled = sbtnSetTime.Enabled = true;
                return true;
            }
            catch (Exception ex)
            {
                UI.ShowError(ex.Message);
                return false;
            }
            finally
            {
                EndWaiting();
            }
        }

        private void WriteToIni()
        {
            Session.Finger_IP = txtIP.Text.Trim();
            Session.Finger_Port = Convert.ToInt32(spePort.Value);
            Session.Finger_IsConneted = true;
            var ini = new INIHelper(Consts.IniConfigPath);
            ini.Writue("Finger", "IP", txtIP.Text.Trim());
            ini.Writue("Finger", "Port", Convert.ToInt32(spePort.Value).ToString());
        }
        private En_Result TestConnection()
        {
            //var helper = new ZKTHelper();
            return ZKTHelper.ZKTConnectTest(txtIP.Text.Trim(), Convert.ToInt32(spePort.Value));
        }
        private bool CheckInput()
        {
            base.errorPop.ClearErrors();
            var ipEx = @"^([1-9]|[1-9]\d|(1\d{2}|2[0-5][0-5]))\.([0-9]|[1-9]\d|(1\d{2}|2[0-5][0-5]))\.([0-9]|[1-9]\d|(1\d{2}|2[0-5][0-5]))\.([0-9]|[1-9]\d|(1\d{2}|2[0-5][0-5]))$";
            if (string.IsNullOrEmpty(txtIP.Text) || !new Regex(ipEx).IsMatch(txtIP.Text))
            {
                base.errorPop.SetError(txtIP, "IP地址格式不正确");
                return false;
            }
            if (spePort.Value < 0 || spePort.Value > 65535)
            {
                base.errorPop.SetError(spePort, "端口超出范围");
                return false;
            }
            return true;
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        { 
            SaveToINI();
        }
        private void sbtnReset_Click(object sender, EventArgs e)
        {
            var res = ZKTHelper.ZKTRestartDevice();
            if (!res.IsSuccess)
                UI.ShowError(res.Message);
            else
            {
                lblStatus.Text = "未连接";
                UI.ShowInfo("重启成功\r\n请稍后重新连接");
                Session.Finger_IsConneted = false;
            }
        }

        //private void sbtnPowerOff_Click(object sender, EventArgs e)
        //{
        //    var res = new ZKTHelper().ZKTPowerOffDevice();
        //    if (!res.IsSuccess)
        //        UI.ShowError(res.Message);
        //    else
        //    {
        //        UI.ShowInfo("禁止输入成功");
        //    }
        //}

        private void sbtnSetTime_Click(object sender, EventArgs e)
        {
            var res = ZKTHelper.ZKTSetDeviceTime();
            if (!res.IsSuccess)
                UI.ShowError(res.Message);
            else
            {
                UI.ShowInfo("同步本机时间成功");
            }
        }

        private void FormFingerSetting_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (Session.Finger_IsConneted)
                ZKTHelper.ZKTDisConnect();
        }
    }
}
